package com.sgam.mf.service.sec;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sgam.mf.dao.ref.RefServicesDAO;
import com.sgam.mf.dao.sec.SecUserDAO;
import com.sgam.mf.domain.ref.RefServices;
import com.sgam.mf.domain.sec.SecUser;
import com.sgam.mf.domain.sec.SecUserProcentDetail;
import com.sgam.mf.helper.ProxyCurrentUser;
import com.sgam.mf.service.GenericServiceImpl;

@Service
public class SecUserServiceImpl extends GenericServiceImpl<SecUser, SecUserDAO>
		implements SecUserService {

	protected final Logger log = Logger.getLogger(getClass());
	
	@Autowired
	public SecUserServiceImpl(SecUserDAO tDao) {
		super(tDao);
	}

	@Autowired
	private RefServicesDAO refServicesDAO;
	
	@Transactional
	public Integer getCurrentUserId() {
		Integer result;
		 Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		 if (auth==null){
			 return 1; // для тестеров
		 }
		 
		 String currentUserName  = auth.getName(); //get logged in username
		 log.debug("currentUserName = " +currentUserName);
		 if (currentUserName.equalsIgnoreCase("anonymousUser")) {
			 result =  -1;
		 } else {
			 result = gettDao().getByName(currentUserName).getId();
		 }
		 return result;
    } 
	
	@Transactional
	public SecUser getNewUser(){
		SecUser result = new SecUser();
		List<SecUserProcentDetail> secUserProcentDetailList = new ArrayList<SecUserProcentDetail>();
		List<RefServices> refServicesList = refServicesDAO.list();
		
		//Collections.sort(refServicesList);
		Collections.sort(refServicesList, new Comparator<RefServices>() {
	        @Override
	        public int compare(final RefServices object1, final RefServices object2) {
	            return object1.getVisualOrder().compareTo(object2.getVisualOrder());
	        }
	       } );
		
		for (RefServices tempItem : refServicesList) {
			SecUserProcentDetail newItem = new SecUserProcentDetail(tempItem);
			secUserProcentDetailList.add(newItem);
     	}
		
		result.setSecUserProcentDetail(secUserProcentDetailList);
		
		return result;
	}
	
	@Transactional
	public ProxyCurrentUser getProxyCurrentUser() {
		ProxyCurrentUser result = null;
		 Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		 if (auth==null){
			 return new ProxyCurrentUser(); // для тестеров
		 }
		 
		 String currentUserName  = auth.getName(); //get logged in username
		 
		 log.debug("currentUserName = " +currentUserName);
		 if (currentUserName.equalsIgnoreCase("anonymousUser")) {
			 result =  new ProxyCurrentUser();
		 } else {
			 result = new ProxyCurrentUser(gettDao().getByName(currentUserName));
		 }
		 return result;
    } 
	
}
